<?php

namespace App\Admin\Controllers;

use App\Models\OrderPhone;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\MessageBag;

class OrderPhoneController extends Controller
{
    use HasResourceActions;

    //编辑子订单
    public function edit($id, Content $content)
    {
        $info = DB::table('order_phones')->where('id', $id)->first();
        $order = DB::table('orders')->where('id', $info->o_id)->first();
        $is_empty = '0';
        $is_fully_confirmed = $order->is_fully_confirmed;
        $is_recheck_confirmed = $order->is_recheck_confirmed;
        $is_confirm = $info->is_confirm;

        return $content->header('子订单')->description('编辑')->body($this->form('', $is_empty, $is_fully_confirmed, $is_recheck_confirmed, $is_confirm)->edit($id));
    }

    //编辑子订单
    public function update($id)
    {
        $info = DB::table('order_phones')->where('id', $id)->first();
        $order = DB::table('orders')->where('id', $info->o_id)->first();
        $is_empty = '0';
        $is_fully_confirmed = $order->is_fully_confirmed;
        $is_recheck_confirmed = $order->is_recheck_confirmed;
        $is_confirm = $info->is_confirm;

        return $this->form('', $is_empty, $is_fully_confirmed, $is_recheck_confirmed, $is_confirm)->update($id);
    }

    //添加子订单
    public function create(Request $request)
    {
        $o_id = $request->get('o_id');
        return Admin::content(function (Content $content) use($o_id) {
            $content->header('创建子订单');
            $content->body($this->form($o_id));
        });
    }


    //添加子订单保存回调
    public function store(Request $request)
    {
        $o_id = $request->get('o_id');
        return $this->form($o_id)->store();
    }

    //修改表单事项
    protected function form($o_id = '', $is_empty = '1', $is_fully_confirmed = '0', $is_recheck_confirmed = '0', $is_confirm = '0')
    {
        // 创建一个表单
        return Admin::form(OrderPhone::class, function (Form $form) use($o_id, $is_empty, $is_fully_confirmed, $is_recheck_confirmed, $is_confirm) {
            if($is_confirm == 3) {
                $form->select('logistics_company', '已退回订单选择物流公司')->options(function ($id) {
                    $logistics = DB::table('logistics_companies')->where('is_show', '1')->get()->toArray();
                    $return = array();
                    foreach ($logistics as $k => $v) {
                        $return[$v->name] = $v->name;
                    }
                    return $return;
                });
                $form->text('tracking_number', '已退回订单选择物流单号');
            }else{
                $form->hidden('o_id')->default($o_id);
                $form->hidden('title');
                $form->text('phone_model', '手机型号');
                $form->text('note', '备注');
                $form->text('imei', 'IMEI码')->rules('required');
                $form->text('level', '等级');
                $form->text('code', '机器标码');
                if($is_fully_confirmed == '1') {
                    $form->currency('price', '价格')->symbol('￥')->rules('required');
                }
            }

            $form->saving(function (Form $form) use ($is_confirm) {
                if($is_confirm != 3) {
                    $form->title = $form->phone_model;
                }
                $imei = DB::table('order_phones')->where('imei', $form->imei)
                    ->where('id', '<>',$form->model()->id)->first();
                if(!empty($imei)) {
                    $error = new MessageBag([
                        'title'   => 'IMEI重复',
                        'message' => 'IMEI码已存在，请校验后输入',
                    ]);

                    return back()->with(compact('error'));
                }
            });

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saved(function (Form $form) use ($o_id, $is_confirm, $is_fully_confirmed, $is_recheck_confirmed, $is_empty) {
                if(empty($o_id)) {
                    $o_id = $form->o_id;
                }
                if($is_confirm == 3) {
                    return redirect('/admin/acknowledged_orders/'.$o_id);
                }elseif($is_fully_confirmed == 1 && $is_recheck_confirmed == 0) {
                    return redirect('/admin/recheck_orders/'.$o_id);
                }else{
                    return redirect('/admin/trial_orders/'.$o_id);
                }
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
                $tools->disableList();
                $tools->disableView();
            });
        });
    }
}
